<?php
/**
 * Created by PhpStorm.
 * User: luoshuqi
 * Date: 19-2-15
 * Time: 下午3:47
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\SysGroup;
use App\Models\SysOperation;
use App\Models\SysUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SysGroupController extends Controller
{
    /**
     * @operation(用户组, 查看, 10000)
     * @return array
     */
    public function get()
    {
        $query = SysGroup::query();
        $name = $this->filter('name');
        $name and $query->where('name', 'LIKE', "%$name%");

        $total = $query->count();
        $list = $query->forPage(...$this->page())->orderByDesc('id')->get();
        return $this->ok(['total' => $total, 'list' => $list]);
    }

    /**
     * @operation(管理员, 创建)
     * @operation(管理员, 更新)
     * @return array
     */
    public function list()
    {
        $list = SysGroup::query()->select('id', 'name')->orderByDesc('id')->get();
        return $this->ok($list->toArray());
    }

    /**
     * @operation(用户组, 创建)
     * @param Request $request
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function create(Request $request)
    {
        list('name' => $name) = $this->required($request, 'name');
        $operation_ids = $request->json('operation_ids');

        SysGroup::query()->forceCreate(['name' => $name, 'operation_ids' => $operation_ids]);
        return $this->ok();
    }


    /**
     * @operation(用户组, 更新)
     * @param $id
     * @return array
     */
    public function find($id)
    {
        $group = SysGroup::query()->findOrFail($id);
        return $this->ok($group->toArray());
    }

    /**
     * @operation(用户组, 更新)
     * @param Request $request
     * @param $id
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(Request $request, $id)
    {
        list('name' => $name) = $this->required($request, 'name');
        $operation_ids = $request->json('operation_ids');

        $group = SysGroup::query()->findOrFail($id);
        $group['name'] = $name;
        $group['operation_ids'] = $operation_ids;
        $group->save();

        return $this->ok();
    }

    /**
     * @operation(用户组, 删除)
     * @param $ids
     * @return array
     */
    public function delete($ids)
    {
        $ids = explode(',', $ids);
        DB::transaction(function () use ($ids) {
            SysGroup::destroy($ids);
            SysUser::deleteByGroupId(...$ids);
        });
        return $this->ok();
    }

    /**
     * @operation(用户组, 创建)
     * @operation(用户组, 更新)
     */
    public function getOperation() {
        $rows = SysOperation::query()
            ->select('id', 'name', 'group')
            ->orderBy('sort')
            ->orderBy('id')
            ->get();

        $data = [];
        foreach ($rows as $row) {
            $data[$row['group']][$row['name']] = $row['id'];
        }

        $result = [];
        foreach ($data as $k => $v) {
            $tmp = ['name' => $k];
            $tmp['children'] = [];
            foreach ($v as $k1 => $v1) {
                $tmp['children'][] = ['name' => $k1, 'id' => $v1];
            }
            $result[] = $tmp;
        }

        return $this->ok($result);
    }
}